var t = getApp(), e = t.requirejs("core"), foxui = t.requirejs("foxui");

Page({
    data: {
      icons: t.requirejs("icons"),
      page: 1,
      loading: false,
      loaded: false,
      cate: 1,
      shanghuid: 0,
      keywords: "",
      list: [],
    },
    onLoad: function(e) { 
        if(e.type==0) this.setData({
            cate: "",
        });
        this.setData({
            shanghuid: e.shanghuid,
        })
    },
    onShow: function() {
        this.setData({
            imgUrl: t.globalData.approot
        }),
        this.getList();
    },
    getCouponList: function() {
        var t = this, cate = t.data.cate;
        t.setData({
            loading: true
        }), e.get("sale/newcoupon/getlist", {
            cate: cate,
            shanghuid: t.data.shanghuid,
            page: t.data.page,
        }, function(e) {
            var i = {
                loading: false,
                total: e.total,
                pagesize: e.pagesize,
                show: !0
            };
            e.list.length > 0 && (i.page = t.data.page + 1, i.list = t.data.list.concat(e.list), 
            e.list.length < e.pagesize && (i.loaded = true)), t.setData(i);
        });
    },
    getList: function() {
        var t = this, url = "", cate = t.data.cate;
        if(cate==1) url = "sale/newcoupon/getlist";
        else if(cate=="") url = "sale/newcoupon/my/getlist";
        else if(cate=="used") url = "sale/newcoupon/my/getlist";
        else if(cate=="past") url = "sale/newcoupon/my/getlist";
        t.setData({
            loading: true
        }), e.get(url, {
            shanghuid: t.data.shanghuid,
            type: cate,
            page: t.data.page,
            keywords: t.data.keywords,
        }, function(e) {
            var i = {
                loading: false,
                total: e.total,
                pagesize: e.pagesize,
                show: !0
            };
            e.list.length > 0 && (i.page = t.data.page + 1, i.list = t.data.list.concat(e.list), 
            e.list.length < e.pagesize && (i.loaded = true)), t.setData(i);
        });
    },
    tab: function(p) {
        var cate = p.currentTarget.dataset.cate;
        this.setData({
            list: [],
            page: 1,
            cate: cate,
            loaded: false,
        })
        ,this.getList();
    },
    bindInput: function(p) {
        this.setData({
            keywords: p.detail.value
        });
    },
    bindSearch: function(p) {
        this.setData({
            list: [],
            page: 1,
            loaded: false,
            keywords: p.detail.value,
        })
        ,this.getList();
    },
    url: function(p) {
        var url = "", userinfoOpenid = t.getCache("userinfo_openid");
        if(userinfoOpenid) url = p.currentTarget.dataset.url;
        else url = "/pages/auth/index"
        wx.navigateTo({ url: url, })
    },
    receive: function(p) {
        console.log(p)
        let userinfoOpenid = t.getCache("userinfo_openid"), 
          th = this, couponid = p.currentTarget.dataset.couponid;
        if(!userinfoOpenid) {
          wx.navigateTo({ url: "/pages/auth/index" })
          return;
        }
        wx.showToast({ 
          title: "加载中...", 
          icon: "loading", 
          mask: true 
        })
        e.get("merch/get_coupon", {
          id: couponid,
        }, function(r) {
          if(r.error==0){
            foxui.toast(th, "领取成功");
            
          }else{
            e.alert(r.message);
          }
          wx.hideToast(); 
        });
    },
    call: function(p) {
        let phone = p.currentTarget.dataset.tel;
        wx.makePhoneCall({
          phoneNumber: phone,
          success:function() { console.log("成功！"); },
          fail:function() { console.log("失败！"); }
        })
    },
    loca: function(p) {
        let lat = parseFloat(p.currentTarget.dataset.lat), lng = parseFloat(p.currentTarget.dataset.lng), 
            nme = p.currentTarget.dataset.name, add = p.currentTarget.dataset.address;
        wx.openLocation({
          latitude: lat,
          longitude: lng,
          name: nme,
          address: add
        })
    },
    onReachBottom: function() {
        this.data.loaded || this.data.list.length == this.data.total || this.getList();
    },
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
    },
});